import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from datetime import datetime, timedelta

def analyze_data_files():
    """分析所有数据文件"""
    
    # 1. 分析训练数据
    print("=== 训练数据1.xlsx 分析 ===")
    try:
        df_train = pd.read_excel('1.xlsx')
        print(f"训练数据形状: {df_train.shape}")
        print(f"列名: {df_train.columns.tolist()}")
        print(f"数据类型:\n{df_train.dtypes}")
        
        if len(df_train) > 0:
            print(f"前几行数据:")
            for i in range(min(5, len(df_train))):
                print(f"  {df_train.iloc[i].tolist()}")
        
        # 检查空值
        print(f"\n空值统计:")
        null_counts = df_train.isnull().sum()
        print(null_counts)
        
        # 详细分析每列
        for col in df_train.columns:
            print(f"\n--- 列 '{col}' 分析 ---")
            print(f"数据类型: {df_train[col].dtype}")
            print(f"空值数量: {df_train[col].isnull().sum()}")
            print(f"唯一值数量: {df_train[col].nunique()}")
            
            if df_train[col].dtype in ['int64', 'float64']:
                print(f"描述统计:")
                print(df_train[col].describe())
            elif df_train[col].dtype == 'object':
                print(f"前10个唯一值:")
                print(df_train[col].value_counts().head(10))
        
    except Exception as e:
        print(f"读取训练数据时出错: {e}")
        print("尝试使用不同的读取方式...")
        try:
            df_train = pd.read_excel('1.xlsx', engine='openpyxl')
            print(f"使用openpyxl引擎读取成功，形状: {df_train.shape}")
            print(f"列名: {df_train.columns.tolist()}")
        except Exception as e2:
            print(f"使用openpyxl也失败: {e2}")
            df_train = None
    
    # 2. 分析预测数据
    print("\n=== 预测数据2.xlsx 分析 ===")
    try:
        df_pred = pd.read_excel('2.xlsx')
        print(f"预测数据形状: {df_pred.shape}")
        print(f"列名: {df_pred.columns.tolist()}")
        
        if len(df_pred) > 0:
            print(f"前几行数据:")
            for i in range(min(5, len(df_pred))):
                print(f"  {df_pred.iloc[i].tolist()}")
                
    except Exception as e:
        print(f"读取预测数据时出错: {e}")
        df_pred = None
    
    # 3. 分析结果文件
    print("\n=== 结果文件3.xlsx 分析 ===")
    try:
        df_result = pd.read_excel('3.xlsx')
        print(f"结果文件形状: {df_result.shape}")
        print(f"列名: {df_result.columns.tolist()}")
        
        if len(df_result) > 0:
            print(f"前几行数据:")
            for i in range(min(5, len(df_result))):
                print(f"  {df_result.iloc[i].tolist()}")
                
    except Exception as e:
        print(f"读取结果文件时出错: {e}")
        df_result = None
    
    return df_train, df_pred, df_result

if __name__ == "__main__":
    df_train, df_pred, df_result = analyze_data_files()
    
    print("\n=== 总结 ===")
    if df_train is not None:
        print(f"训练数据: {len(df_train)} 条记录")
    if df_pred is not None:
        print(f"预测数据: {len(df_pred)} 条记录") 
    if df_result is not None:
        print(f"结果文件: {len(df_result)} 条记录")